<?php

namespace app\api\controller\shop;


use app\admin\model\Shopcamera;
use app\admin\model\Shopdevice;
use app\admin\model\Shoproom;
use app\common\controller\Api;
use fast\Http;

/**
 *
 */
class Device extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 获取店铺设备总数
     *
     */
    public function getData()
    {
        $ret = array();
        $shop_id = $this->request->param('shop_id');
        $shopDeviceModel = new Shopdevice();
        $total = $shopDeviceModel->where('shop_id',$shop_id)->count('id');
        $online = $shopDeviceModel->where('shop_id',$shop_id)->where('status','online')->count('id');

        $ret['total'] = $total;
        $ret['online'] = $online;
        $ret['offline'] = $total-$online;

        $this->success('请求成功',$ret);
    }

    /**
     * 设备列表
     */
    public function deviceList(){

        $shop_id = $this->request->param('shop_id');
        $page = $this->request->param('page');
        $status = $this->request->param('status');
        $bind = $this->request->param('bind');

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $deviceModel = new Shopdevice();

        $user = $this->auth->getUser();
        $where = array();
        $where['Shopdevice.user_id'] = $user['id'];
        if($shop_id != ''){
            $where['Shopdevice.shop_id'] = $shop_id;
        }
        if($bind != ''){
            $where['bind_status'] = $bind;
        }
        if($status != ''){
            $where['Shopdevice.status'] = $status;
        }

        $list = $deviceModel->with(['shop','room'])->where($where)->limit($offset,$limit)->select();

        $this->success('',$list);
    }

    public function bindDevice(){

        $shop_id = $this->request->param('shop_id');
        $room_id = $this->request->param('room_id');
        $sn = $this->request->param('sn');

        $user =$this->auth->getUser();

        $deviceModel = new Shopdevice();

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$shop_id)->where('user_id',$user['id'])->find();


        error_log($shop_id."==d==".json_encode($shop).$user['id']);
        if(!$shop) $this->error("无权限操作");

        $d = $deviceModel->where('device_sn',$sn)->find();
        error_log($user['id']."==d==".json_encode($d));

        if($d['user_id'] != $user['id']){
            $this->error("无权限操作");
        }
        if($d['bind_status'] != -1){
            $this->error("先解绑再操作");
        }
        $shopRoomModel = new Shoproom();
        $shopRoomModel->where('id',$room_id)->update(['is_bind'=>1]);
        $deviceModel->where('id',$d['id'])->update(['shop_id'=>$shop_id,'room_id'=>$room_id,'bind_status'=>1]);
        $this->success('成功');
    }

    public function unbindDevice(){

        $room_id = $this->request->param('room_id');
        $deviceModel = new Shopdevice();
        $shopRoomModel = new Shoproom();
        $shopRoomModel->where('id',$room_id)->update(['is_bind'=>-1]);

        $deviceModel->where('room_id',$room_id)->update(['shop_id'=>-1,'room_id'=>-1,'bind_status'=>-1]);

        $this->success('成功');
    }


    public function unbindTableDevice(){
        $room_id = $this->request->param('room_id');
        $deviceModel = new Shopdevice();
        $deviceModel->where('room_id',$room_id)->where('device_type','tq')->update(['shop_id'=>-1,'room_id'=>-1,'bind_status'=>-1]);
        $this->success('成功');

    }


    public function bindDoorDevice(){

        $shop_id = $this->request->param('shop_id');
        $room_id = $this->request->param('room_id');
        $sn = $this->request->param('sn');

        $user =$this->auth->getUser();

        $deviceModel = new Shopdevice();

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$shop_id)->where('user_id',$user['id'])->find();
        if(!$shop) $this->error("无权限操作");

        $d = $deviceModel->where('device_sn',$sn)->find();
        if($d['user_id'] != $user['id']){
            $this->error("无权限操作");
        }

        $type ="后门";
        if($room_id == -1){
            $type = "大门";
        }

        $device = $deviceModel->where('room_id',$room_id)->where('shop_id',$shop_id)->find();
        if($device){
            $deviceModel->where('id',$device['id'])->update(['bind_status'=>-1,'shop_id'=>-1,'room_id'=>-1,'type'=>'包厢']);
        }
        $deviceModel->where('id',$d['id'])->update(['shop_id'=>$shop_id,'room_id'=>$room_id,'bind_status'=>1,'type'=>$type]);
        $this->success('成功');
    }


    public function getCamera(){

        $shop_id = $this->request->param('shop_id');
        $shopCameraModel = new Shopcamera();
        $camera = $shopCameraModel->where('shop_id',$shop_id)->find();
        if($camera){


            $url = "https://open.ys7.com/api/lapp/token/get";
            $req = array();
            $req['appKey'] = "661a7bc3b2f2495482f5e76025bbe08d";
            $req['appSecret'] = "d52010862c6060e0110072a5eb53741a";


            $isRequest = -1;
            $cacheTime = $this->redis->get("cameraKey");
            $cacheValue = $this->redis->get("cameraValue");

            if($cacheTime != "" || $cacheTime != null){
                $time = strtotime("-30 minute");
                $startDate =date("Y-m-d H:i:s",$time).'.123'; // 指定日期
                $startTimestamp = (float) sprintf('%.0f', strtotime($startDate) * 1000 + explode(' ', microtime())[0] * 1000);

                if($cacheTime > $startTimestamp){
                    $isRequest = 1;
                }
            }

            if($isRequest == -1){
                $result = Http::post($url,$req);
                $result = json_decode($result,true);
                $token = "";
                if($result['code'] == 200){
                    $token  =  $result['data']['accessToken'];
                    $expireTime = $result['data']['expireTime'];
                    $this->redis->set("cameraKey",$expireTime);
                    $this->redis->set("cameraValue",$token);
                }else{
                    $this->error("请求Token失败，请联系技术");
                }
            }else{
                $token = $cacheValue;
            }
            $camera['token'] = $token;
            $this->success("",$camera);


        }else{
            $this->error("请联系技术购买摄像头，才能实用","",-2);
        }
    }

}
